|
|
|
|
|
|
|
Note that, in addition to returning the salesperson with no customers, the RIGHT JOIN returned all customer records for each of the other salespeople, not just a single record. This is because a RIGHT JOIN is designed to return all the records from the right table, even if they have no corresponding record in the left table. |
|
|
|
|
|
|
|
|
You can use the WHERE clause to relate two tables. The WHERE clause has the same effect as an INNER JOIN. Listing C.8 shows the same INNER JOIN as Listing C.7, this time using the WHERE clause instead of the INNER JOIN. |
|
|
|
|
|
|
|
|
Listing C.8 WHERE.TXTA WHERE Clause Performing the Same Function as an INNER JOIN |
|
|
|
|
|
|
|
|
'***************************************
'Select using WHERE to relate two tables
'***************************************
SELECT CS.Lastname, CS.Firstname, SL.Saleslast, SL.Salesfirst _
FROM Customers AS CS, Salesmen AS SL, _
WHERE CS.SalesID=SL.SalesID |
|
|
|
|
|
|
|
|
Setting the Filter Criteria |
|
|
|
|
|
|
|
|
One of the most powerful features of SQL commands is that you can control the range of records to be processed by specifying a filter condition. You can use many types of filters, such as Lastname = "Smith", Price < 1, or birthday between 5/1/94 and 5/31/94. Although the current discussion is specific to the use of filters in the SELECT command, the principles shown here also work with other SQL commands, such as DELETE and UPDATE. |
|
|
|
|
|
|
|
|
Filter conditions in a SQL command are specified using the WHERE clause. The general format of the WHERE clause is as follows: |
|
|
|
|
|
|
|
|
There are four types of predicates (logical statements that define the condition) that you can use with the WHERE clause. These are shown in the following table: |
|
|
|
|
| | | |
|
|
|
|
Compares a field to a given value |
|
|
|
| | |
|
|
|
|
Compares a field to a pattern (for example, A*) |
|
|
|
| | |
|
|
|
|
Compares a field to a list of acceptable values |
|
|
|
| | |
|
|
|
|
Compares a field to a range of values |
|
|
|
|
|
|
|
|
|
|
Using the Comparison Predicate |
|
|
|
|
|
|
|
|
As its name suggests, the comparison predicate is used to compare the values of two expressions. There are six comparison operators (the symbols that describe the comparison type) that you can use; the operators and their definitions are summarized in Table C.3. |
|
|
|
|
|